package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.MmrwT;
import cn.e4j.modular.crm.service.impl.MmrwTServiceImpl;
import cn.e4j.modular.erp.controller.ErpLogic;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 维修费用结算
 * Created by miaoyl on 2019-08-30
 */
@Controller
@RequestMapping("/crm/gwei024")
public class Gwei024Controller extends BaseController {

    @Autowired
    private MmrwTServiceImpl mmrwTService;

    @Autowired
    private ErpLogic erpLogic;

    /**
     * 跳转维修费用结算管理页面
     */
    @RequiresPermissions("crm:gwei024:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwei024.html";
    }

    /**
     * 查询维修费用结算信息列表
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("crm:gwei024:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrwTService.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwei024:add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrwT> mmrwTList = inDto.getReqList(MmrwT.class);
        mmrwTService.addData(mmrwTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwei024:edit")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrwT> mmrwTList = inDto.getReqList(MmrwT.class);
        mmrwTService.editData(mmrwTList);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwei024:del")
    @ResponseBody
    public ResponseData del(MmrwT mmrwT) {
        mmrwTService.delete(mmrwT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwei024:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrwT> mmrwTList = inDto.getReqList(MmrwT.class);
        mmrwTService.delDataBatch(mmrwTList);
        return SUCCESS_TIP;
    }


    /**
     * 修改状态
     */
    @RequestMapping("/updateStatus")
    @RequiresPermissions("crm:gwei024:init")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrwT> mmrwTList = inDto.getReqList(MmrwT.class);
        mmrwTService.updateStatus(mmrwTList);
        return SUCCESS_TIP;
    }

    /**
     * 报工信息开窗
     */
    @RequestMapping("/getMmrhgData")
    @ResponseBody
    public ResponseData getMmrhgData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrwTService.getMmrhgData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增数据
     */
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        outDto.put("MMRWDOCDATE", DateUtils.formatDate(new Date(), DateUtils.PATTERN_yyyy_MM_dd));
        outDto.put("MMRWDOCNO", "PFY" + DateUtils.getDate("yyyyMMddHHmmssSSS"));
        return ResponseData.success(outDto);
    }

    /**
     * 归属法人信息开窗
     */
    @RequestMapping("/getSysOrg")
    @ResponseBody
    public ResponseData getSysOrg(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrwTService.getSysOrg(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 根据编号获取信息
     */
    @RequestMapping("/getMmrwData")
    @ResponseBody
    public ResponseData getMmrwData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrwTService.getMmrwData(inDto);
        return ResponseData.success(result);
    }

    /**
     * 获取ERP组织名称
     */
    @RequestMapping("/getMB002")
    @ResponseBody
    public ResponseData getMB002(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = mmrwTService.getMB002(inDto);
        return ResponseData.success(result);
    }

    /**
     * 报工单转入
     */
    @RequestMapping("/transferMmrghData")
    @RequiresPermissions("crm:gwei024:init")
    @ResponseBody
    public ResponseData transferMmrghData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrwT> mmrwTList = inDto.getReqList(MmrwT.class);
        Dto result = mmrwTService.transferMmrghData(mmrwTList);
        return ResponseData.success(result);
    }

    /**
     * 转erp订单
     */
    @RequestMapping("/transferERP")
    @RequiresPermissions("crm:gwei024:init")
    @ResponseBody
    public ResponseData transferERP(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        //JSONArray result = mmrwTService.transferERP(inDto);
        JSONArray result = new JSONArray();
        try {
            result = erpLogic.getERPService("transfer024",inDto);
        }catch (NoSuchMethodException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseData.success(result);
    }

    /**
     * 还原erp订单
     */
    @RequestMapping("/restoreERP")
    @RequiresPermissions("crm:gwei024:restoreERP")
    @ResponseBody
    public ResponseData restoreERP(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        //Dto result = mmrwTService.restoreERP(inDto);
        Dto result = Dtos.newDto();
        try {
            result = erpLogic.getERPService("restore024",inDto);
        }catch (NoSuchMethodException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseData.success(result);
    }

    /**
     * 获取仓库
     */
    @RequestMapping("/getGwas001")
    @ResponseBody
    public ResponseData getGwas001(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = mmrwTService.getGwas001(inDto);
        return ResponseData.success(result);
    }
}
